package com.zhe.zheblogmaster.service.impl;

import com.zhe.zheblogmaster.entity.AdminUser;
import com.zhe.zheblogmaster.mapper.AdminUserMapper;
import com.zhe.zheblogmaster.service.AdminUserService;
import com.zhe.zheblogmaster.util.ZheBlogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author WHAOU
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {
    @Autowired
    private AdminUserMapper userMapper;

    @Override
    public AdminUser login(String userName, String password) {
        String encode = ZheBlogUtils.md5Encode(password);
        return userMapper.login(userName, encode);
    }

    @Override
    public AdminUser getUserInfo() {
        return userMapper.getUserInfo().get(0);
    }

    @Override
    public int updateName(Integer loginUserId, String userName, String nickName) {
        AdminUser user = userMapper.selectByPrimaryKey(loginUserId);
        if (user == null) {
            return 0;
        }
        user.setLoginUserName(userName);
        user.setNickName(nickName);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int updatePassword(Integer loginUserId, String originalPassword, String newPassword) {
        AdminUser user = userMapper.selectByPrimaryKey(loginUserId);
        if (user != null) {
            String originalPsdMd5 = ZheBlogUtils.md5Encode(originalPassword);
            if (originalPsdMd5 != null && originalPsdMd5.equals(user.getLoginPassword())) {
                user.setLoginPassword(ZheBlogUtils.md5Encode(newPassword));
                return userMapper.updateByPrimaryKeySelective(user);
            }
        }
        return 0;
    }
}
